//
//  PowerListCell.swift
//  SCA
//
//  Created by tjun on 2025/9/7.
//

import UIKit

 

class PowerListInfoView: UIView {
    
    private let iconImageView = UIImageView()
    private let titleLabel = UILabel(text: "", font: UIFontSemiboldMake(20), textColor: .white)
    private let subTitleLabel = UILabel(text: "", font: UIFontMake(12), textColor: .white)
  
    private let amountLabel = UILabel(text: "总购买金额".localizedString, font: UIFontMake(12), textColor: .white)
    private let amountValueLabel = UILabel(text: "", font: UIFontSemiboldMake(16), textColor: .white)
    
    private let yearsRateLabel = UILabel(text: "年利率".localizedString, font: UIFontMake(12), textColor: .white)
    private let yearsRateValueLabel = UILabel(text: "", font: UIFontSemiboldMake(16), textColor: .white)

    var model: PowerListModel! {
        didSet {
            iconImageView.setImage(url: model.icon)
            titleLabel.text = model.title
            subTitleLabel.text = model.subtitle
            amountValueLabel.text = model.totalDisp
            yearsRateValueLabel.text = model.aprDisp
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubviews(iconImageView, titleLabel, subTitleLabel, amountLabel, amountValueLabel, yearsRateLabel, yearsRateValueLabel)
        iconImageView.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(40)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImageView.snp.right).offset(14)
            make.top.equalTo(iconImageView)
        }
        
        subTitleLabel.snp.makeConstraints { make in
            make.left.equalTo(iconImageView.snp.right).offset(14)
            make.bottom.equalTo(iconImageView)
        }
        
        amountLabel.snp.makeConstraints { make in
            make.right.equalTo(-94)
            make.top.equalTo(26)
        }
        
        amountValueLabel.snp.makeConstraints { make in
            make.right.equalTo(-12)
            make.centerY.equalTo(amountLabel)
        }
        
        yearsRateLabel.snp.makeConstraints { make in
            make.right.equalTo(amountLabel)
            make.top.equalTo(amountLabel.snp.bottom).offset(10)
        }
        
        yearsRateValueLabel.snp.makeConstraints { make in
            make.right.equalTo(amountValueLabel)
            make.centerY.equalTo(yearsRateLabel)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

class PowerListCell: BaseTableViewCell {

    private let infoView = PowerListInfoView()
    private let descriptionLabel = UILabel()
    private let iconPlusView = IconPlusView()
    private let containerView = UIView()

    var data: PowerListData? {
        didSet {
            guard let data = data else { return }
            descriptionLabel.attributedText = data.attributedText
            infoView.model = data.model
            iconPlusView.configure(with: data.model.icons)
        }
    }
    
    override func setSubView() {
        super.setSubView()
        
        selectionStyle = .none
        
        backgroundColor = AppStyle.color.background
        contentView.addSubview(containerView)
        containerView.addCorner(radius: 10)
        containerView.backgroundColor = UIColor.black
        containerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(AppStyle.margin.left)
            make.top.equalToSuperview()
            make.bottom.equalTo(-AppStyle.margin.left)
        }

        descriptionLabel.numberOfLines = 0
        containerView.addSubview(infoView)
        containerView.addSubview(descriptionLabel)
        containerView.addSubview(iconPlusView)

    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        guard let data = data else { return }
        
        infoView.frame = data.infoViewRect
        descriptionLabel.frame = data.descriptionRect
        iconPlusView.frame = data.iconsRect
    }

    
}


class PowerInfoView: UIView {
    
    let castingPowerView = PowerRowView()
    let matrixPowerView = PowerRowView()
    let differentialPowerView = PowerRowView()
    let levelPowerView = PowerRowView()
    var onPowerRowViewInfoTapped: ((PowerModel) -> Void)?
    
    var powerInfo: PowerInfo? {
        didSet {
            guard let info = powerInfo,
                  let mintingInfo = info.cat[.minting],
                  let matrixInfo = info.cat[.matrix],
                  let differentialInfo = info.cat[.differential],
                  let peerInfo = info.cat[.peer] else { return }
            
            castingPowerView.model = mintingInfo
            matrixPowerView.model = matrixInfo
            differentialPowerView.model = differentialInfo
            levelPowerView.model = peerInfo
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        let titleView = UIView().addTo(self)
        titleView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(36)
        }
        
        let titleLabel = UILabel(text: "总算力".localizedString, font: UIFontMake(12), textColor: .white).addTo(titleView)
        titleLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(16)
        }
        
        let lineView = UIView().addTo(titleView)
        lineView.backgroundColor = UIColorMake(0xAFB2B7).withAlphaComponent(0.5)
        lineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview()
            make.height.equalTo(0.7)
        }
        
        castingPowerView.addTo(self)
        castingPowerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(16)
            make.top.equalTo(titleView.snp.bottom).offset(14)
            make.height.equalTo(22)
        }
        
        matrixPowerView.addTo(self)
        matrixPowerView.snp.makeConstraints { make in
            make.left.right.height.equalTo(castingPowerView)
            make.top.equalTo(castingPowerView.snp.bottom)
        }
        
        differentialPowerView.addTo(self)
        differentialPowerView.snp.makeConstraints { make in
            make.left.right.height.equalTo(castingPowerView)
            make.top.equalTo(matrixPowerView.snp.bottom)
        }
        
        levelPowerView.addTo(self)
        levelPowerView.snp.makeConstraints { make in
            make.left.right.height.equalTo(castingPowerView)
            make.top.equalTo(differentialPowerView.snp.bottom)
        }
        
        castingPowerView.value = "0.00 G"
        matrixPowerView.value = "0.00 G"
        differentialPowerView.value = "0.00 G"
        levelPowerView.value = "0.00 G"
        
        castingPowerView.infoButton.addTarget(self, action: #selector(powerRowInfoViewTapped(_ :)), for: .touchUpInside)
        matrixPowerView.infoButton.addTarget(self, action: #selector(powerRowInfoViewTapped(_ :)), for: .touchUpInside)
        differentialPowerView.infoButton.addTarget(self, action: #selector(powerRowInfoViewTapped(_ :)), for: .touchUpInside)
        levelPowerView.infoButton.addTarget(self, action: #selector(powerRowInfoViewTapped(_ :)), for: .touchUpInside)
    }
    
    
    @objc func powerRowInfoViewTapped(_ button: UIButton) {
        guard let powerView = button.superview as? PowerRowView,
              let model = powerView.model else { return }
        
        onPowerRowViewInfoTapped?(model)
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


